<?php


class DistributorController extends Yaf_Controller_Abstract {

	public function getDistributorListAction(){
		$request=json_decode(file_get_contents("php://input"),true);
		$page = $request['page'] ? $request['page'] : 1;
		$pagesize = $request['pagesize'] ? $request['pagesize'] : 10;

		$where = [];
		
		$model = new DistributorModel();
		$list = $model->getList($where, $page, $pagesize);
		foreach($list as &$val){
			if($val['ctime']){
				$val['ctime'] = date('Y-m-d H:i', $val['ctime']);
			}else{
				$val['ctime'] = '暂无数据';
			}
			
		}
		$count = $model->count($where);
		
		echo json_encode(['total' => $count, 'list' => $list]);
	}
	
	public function getDistributorAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		$distributor_id = $request['distributor_id'];
		
		$model = new DistributorModel();
		$where = [];
		if($distributor_id){
			$where['distributor_id = ?'] = $distributor_id;
		}
		
		$rs = $model->getOne($where);
		
		echo json_encode($rs);
	}
	
	public function saveDistributorAction(){
		$request = json_decode(file_get_contents("php://input"),true);
		if(!$request) return;
		$model = new DistributorModel();

		$data = [
			'name'     => $request['name'],
			'mobile'   => $request['mobile'],
			'truename' => $request['truename'],
			'integral' => $request['integral'],
			'balance'  => $request['balance'],
		];
		if($request['distributor_id']){
			$where['distributor_id = ?'] = $request['distributor_id'];
			
			$rs = $model->save($data, $where);
		}else{
			$rs = $model->add($data);
		}
		echo json_encode($rs);
	}
}
